var orderId = $Util.getQueryString(document.location.search, "id");
$(document).ready(function() {
	$Base.isLoginManage(getPick);
	$('#print').click(function(){
		window.print();
	});
	window.onbeforeprint = function(){
		$('#no_print').hide();
		var id = $('#id').val();
		var url = '/json?action=MANAGE_UPDATE_PRINT_DATE_BY_ID_ACTION&ID='+id;
		$.ajaxSetup({async: false});
		$.post(url,{},function(data){
			if(data.success){
				var date = data.root.DATE.rs[0].time;
				$('#print_date').html($Util.toChar(date, 'yyyy-mm-dd hh24:mi:ss'));
				$.ajaxSetup({async: true});
			}
			else{
				$.zxxbox.msg(data.errors.errmsg, 2000, 'error');
			}
		},'json');
	};
	window.onafterprint = function(){
		$('#no_print').show();
	};
});

function getPick(){
	var url = '/json?action=MANAGE_GET_DELIVER_ORDER_BY_ORDER_ID_ACTION&ORDER_ID='+orderId;
	$.post(url, {}, function(data){
		if(data.success){
			var obj = data.root.T_DELIVER_ORDER;
			if(obj == undefined){
				$.zxxbox.msg("订单"+orderId+"还未生成配送单，无法打印", 0, 'error', {bgclose:false});
			}
			obj = obj.rs[0];
			var productList = JSON.parse(obj.PRODUCT_LIST);
			var list = '';
			$.each(productList, function(i, o){
				list += '<tr>';
				list += '<td align="center">' + o.PRODUCT_NO + '</td>';
				list += '<td colspan="2">' + o.PRODUCT_NAME + '</td>';
				list += '<td align="center">￥' + (o.MEMBER_PRICE).toFixed(2) + '</td>';
				list += '<td align="center">' + o.QTY + '</td>';
				list += '<td align="center">￥' + (o.AMOUNT).toFixed(2) + '</td>';
				list += '</tr>';
			});
			$('#list').html(list);
			$('#id').val(obj.ID);
			$('#deliver_id').html(obj.ID);
			$('#order_id').html(obj.ORDER_ID);
//			$('#print_date').html($Util.toChar(obj.PRINT_DATE, 'yyyy-mm-dd hh24:mi:ss'))
			$('#member').html(obj.MEMBER_ACCOUNT);
			$('#deliver').html($Base.getDeliverMethod(obj.DELIVER_METHOD) + " " + $Base.getDeliverTime(obj.DELIVER_TIME));
			$('#consignee_name').html(obj.MEMBER_NAME);
			$('#phone').html(obj.PHONE);
			$('#address').html(obj.ADDRESS);
			$('#remark').html(obj.MESSAGE);
			$('#amount').html((obj.PAYMENT).toFixed(2));
		}
		else{
			$.zxxbox.msg(data.errors.errmsg, 2000, 'error');
		}
	}, 'json');
}